package com.vayne.injectdemo;

import com.vayne.bean.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Collection;

/**
 * @author xuerui.zhang
 * @since 2020/4/15 上午10:30
 */
public class QualifierInjectionDemo {

    @Autowired
    private Course category;

    @Autowired
    @Qualifier("course")
    private Course course;

    @Autowired
    private Collection<Course> allCourse;

    @Autowired
    @Qualifier
    private Collection<Course> qualifierCourse;

    @Bean
    @Qualifier
    public Course course1() {
        return Course.creteCourse();
    }

    @Bean
    @Qualifier
    public Course course2() {
        return Course.creteCourse();
    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(QualifierInjectionDemo.class);

        XmlBeanDefinitionReader beanDefinition = new XmlBeanDefinitionReader(applicationContext);
        String path = "classpath:/dependency_lookup_config.xml";
        beanDefinition.loadBeanDefinitions(path);

        applicationContext.refresh();
        QualifierInjectionDemo demo = applicationContext.getBean(QualifierInjectionDemo.class);

        System.out.println(demo.category);
        System.out.println(demo.course);
        System.out.println(demo.allCourse);
        System.out.println(demo.qualifierCourse);

        applicationContext.close();
    }
}
